<template>
	<div v-cloak class="aui-wrapper aui-page__iframe">
		<div class="aui-content__wrapper">
			<div class="aui-content">
				<el-card shadow="never" class="aui-card--fill">
					<div class="mod-lswms_Move_task">
						<el-form :model="dataFormTask" ref="dataFormTaskRef">
							<el-row :gutter="20">
								<el-col :span="4">
									<el-form-item>
										<el-input v-model="dataFormTask.fromtray" size="small" placeholder="箱"
											@click="openInventoryDialogHandle()">
										</el-input>
									</el-form-item>
								</el-col>

								<el-col :span="4">
									<el-form-item>
										<el-input v-model="dataFormTask.fromlocation" size="small" placeholder="原库位"
											clearable>
										</el-input>
									</el-form-item>
								</el-col>
								<el-col :span="4">
									<el-form-item>
										<el-input v-model="dataFormTask.tolocation" size="small" placeholder="目的库位"
											clearable>
										</el-input>
									</el-form-item>
								</el-col>
							</el-row>
							<el-row>
								<el-col :span="2">
									<el-button size="small" @click="getInventorByLocationAndTray">刷新列表</el-button>
								</el-col>
								<el-col :span="1">
									<el-button size="small" @click="createMoveTask">移库</el-button>
								</el-col>
							</el-row>
						</el-form>
						<div>
							<el-table v-loading="dataListLoading" :data="inventoryDataList" border style="width: 100%;">
								<el-table-column prop="sku" label="物料编码" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="descr" label="物料名称" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="location" label="库位编码" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="tray" label="箱" header-align="center" align="center">
								</el-table-column>
								<!-- <el-table-column prop="box" label="箱" header-align="center" align="center">
								</el-table-column> -->
								<el-table-column prop="boxstr" label="格" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="lot" label="批号" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="qty" label="数量" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="pickqty" label="分配量" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="putqty" label="待上架量" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="status" label="状态" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="remark" label="备注" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="adduser" label="创建人" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="addtime" label="创建时间" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="updatuser" label="更新人" header-align="center" align="center">
								</el-table-column>
								<el-table-column prop="updatetime" label="更新时间" header-align="center" align="center">
								</el-table-column>
							</el-table>
						</div>
						<!-- <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit"
							:total="total" layout="total, sizes, prev, pager, next, jumper"
							@size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
						</el-pagination> -->
					</div>
					<InventoryDialog v-if="dialogInventoryVisible" ref="InventoryDialogVue"
						@filllineInventory="fillLinebyInventory">
					</InventoryDialog>
				</el-card>
			</div>
		</div>
	</div>

</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';

	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"
	import {
		getInventoryList
	} from "@/network/lswms/stock/Inventory.js"
	import {
		putCreateMoveTask
	} from "@/network/lswms/task/Task.js"
	
	import InventoryDialog from "@/components/lswms/stock/InventoryDialog1"
	export default {
		name: "createMoveTask",
		components: {
			InventoryDialog
		},
		setup() {
			const dataFormTask = ref({
				id: '',
				taskkey: '',
				tasktype: '',
				priority: '',
				status: '',
				sku: '',
				qty: '',
				sourcetype: '',
				sourcekey: '',
				sourceline: '',
				sourcelinelist: '',
				fromlocation: '',
				tolocation: '',
				fromtray: '',
				totray: '',
				frombox: '',
				tobox: '',
				fromlot: '',
				tolot: '',
				taskno: '',
				remark: '',
				sendflag: '',
				sendtime: '',
				receivetime: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});

			const dataFormTaskRef = ref(null);

			const dialogInventoryVisible = ref(false);
			const InventoryDialogVue = ref(null);
			const openInventoryDialogHandle = () => {
				// if (dataForm.value.line == "") {
				// 	return ElMessage.error("请先选择出库单明细");
				// }


				dialogInventoryVisible.value = true;
				nextTick(function() {
					InventoryDialogVue.value.dataForm.sku = "";
					InventoryDialogVue.value.init();
				})
			}

			const fillLinebyInventory = (inventoryRow) => {
				dataFormTask.value.fromlocation = inventoryRow.location;
				dataFormTask.value.fromtray = inventoryRow.tray;
				getInventorByLocationAndTray();
			}

			const inventoryDataList = ref([]);
			const dataListLoading = ref(false);
			const getInventorByLocationAndTray = () => {

				if ( !dataFormTask.value.fromtray  &&!dataFormTask.value.fromlocation) {
					return;
				}
				let queryParam = {
					"tray": dataFormTask.value.fromtray,
					"location": dataFormTask.value.fromlocation
				}

				getInventoryList(queryParam).then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					//dataListLoading.value = true
					inventoryDataList.value = res.data.list

				})

				//dataListLoading.value= false;
			}

			//是否锁上  变量
			const timer = ref(true);
			const createMoveTask =()=>{
				if (!dataFormTask.value.fromtray ) {
					return ElMessage({
						message: '请选择箱',
						type: 'warning',
						duration: 1000
					});
				}
				if (!dataFormTask.value.fromlocation ) {
					return ElMessage({
						message: '请选择原库位',
						type: 'warning',
						duration: 1000
					});
				}
				if (!dataFormTask.value.tolocation ) {
					return ElMessage({
						message: '请填写目的库位',
						type: 'warning',
						duration: 1000
					});
				}
				if (timer.value==false){
                    return ElMessage({
                        message: '请不要短时间内多次提交请求(不要点太快)',
                        type: 'error',
                        duration: 1000
                    });
				}
				
				ElMessageBox.confirm('确定要将箱:'+dataFormTask.value.fromtray+' 从库位:'+dataFormTask.value.fromlocation
						+'移到库位:'+dataFormTask.value.tolocation+'吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
				    //开始锁住
                    // timer.value = false;
                    // setTimeout(function () {
					 //    //节流操作
                     //    timer.value = true;
                    // },20000)
					let para =  {
						
							"fromtray": dataFormTask.value.fromtray,
							"fromlocation": dataFormTask.value.fromlocation,
							"tolocation":dataFormTask.value.tolocation
					
					}
					putCreateMoveTask(para).then(function(res) {
					    //开锁
                        timer.value = true;
						if (res.code != 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功,'+res.msg,
							type: 'success',
							duration: 2000,
							onClose: function() {
								getInventorByLocationAndTray();
							}
						});
					}).catch(function() {});
				
				}).catch(function() {})
				
			}

			return {
			    timer,
				dataFormTaskRef,
				dataFormTask,
				dialogInventoryVisible,
				InventoryDialogVue,
				openInventoryDialogHandle,
				fillLinebyInventory,
				inventoryDataList,
				dataListLoading,
				getInventorByLocationAndTray,
				createMoveTask
			}
		}
	}
</script>

<style>
</style>
